﻿using CPPEI.BD.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.BD.Agent
{
    class ForwardVMPackageHandler : CPPEI.Net.BaseResponseHandler<ForwardVMPackageData>
    {
        protected override void OnHandle(Net.NetPackage response, ForwardVMPackageData responseData)
        {
            SendUdpClient(responseData.TargetIP, responseData.TargetPort, responseData.PackageData, responseData.PackageData.Length);
            CPPEI.Coolzon.Common.Logger.Debug("发送视频切换数据到device程序，device:{0}:{1},length:{2}",
                responseData.TargetIP,
                responseData.TargetPort,
                responseData.PackageData.Length);
        }

        private static bool SendUdpClient(string ip, int port, byte[] sendData, int bytes)
        {
            try
            {
                IPAddress hostIP = IPAddress.Parse(ip);
                IPEndPoint host = new IPEndPoint(hostIP, port);
                using (UdpClient udpClient = new UdpClient())
                {
                    udpClient.Send(sendData, bytes, host);
                }
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
            return true;
        }
    }
}
